package org.example.mq;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

public class Producer {
    public static void main(String[] args) throws Exception {
        // Initialize a producer and set the Producer group name
        DefaultMQProducer producer = new DefaultMQProducer("F21010620"); //（1）
        // Set the address of NameServer
        producer.setNamesrvAddr("192.168.233.66:9876");  //（2）
        // Start Producer
        producer.start();
        // Create a message and set the topic, tag, body and so on. The tag can be understood as a label to categorize the message, and RocketMQ can filter the tag on the consumer side.
        Message msg = new Message("F21010620" /* Topic */,
                "TagA" /* Tag */,
                ("Hello Rocket").getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
        );   //（3）
        // Use the producer to send and wait for the result of sending synchronously
        SendResult sendResult = producer.send(msg);   //（4）
        System.out.printf("%s%n", sendResult);
        // Close the producer once it is no longer in use
        producer.shutdown();
    }
}